package cn.gene.timeyan.article.webapi.job;

import cn.gene.timeyan.article.service.TopService;
import cn.gene.timeyan.article.webapi.mapper.ArticleMapper;
import cn.gene.timeyan.common.constant.RedisConstants;
import cn.gene.timeyan.common.exception.TimeyanServiceException;
import cn.gene.timeyan.common.restful.ResponseCode;
import cn.gene.timeyan.common.utils.FormUtils;
import cn.gene.timeyan.pojo.article.vo.SimpleArticleVO;
import cn.gene.timeyan.pojo.ums.vo.UserSimpleVO;
import cn.gene.timeyan.ums.service.IUserDetailService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class ArticleListJob implements Job {
    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private TopService topService;
    @DubboReference
    private IUserDetailService dubboUserDetail;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("time:"+ LocalDateTime.now());
        //获取模块id
        Set<String> set=redisTemplate.keys("articleList:model*:time");
        System.out.println("set:"+set);
        for (String item:set){
            Long modelId=Long.parseLong(item.substring(item.lastIndexOf("l")+1,item.lastIndexOf(":")));
            System.out.println("modelId:"+modelId);
            List<SimpleArticleVO> tops=topService.getTopList(modelId);
            String key= RedisConstants.ARTICLE_LIST+"model"+modelId+":";
            //删除key
            System.out.println(redisTemplate.delete(key+"detail"));
            System.out.println(redisTemplate.delete(key+"time"));
            //重新缓存
            PageHelper.startPage(1,30);
            List<SimpleArticleVO> articles=articleMapper.getArticleList(modelId);
            if (articles==null ||articles.size()==0){
                throw new TimeyanServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"系统繁忙");
            }
            Map<String,Object> map=new HashMap<>();
            //存入缓存
            for (SimpleArticleVO article:articles) {
                //获取文章信息
                if (article.getContent().length()>50){
                    article.setContent(article.getContent().substring(0,50)+"...");
                }else{
                    article.setContent(article.getContent());
                }
                double time= FormUtils.parseTimeByLocalDateTime(article.getCreateTime());
                //获取作者信息
                UserSimpleVO user=dubboUserDetail.selectUserCommentVOByUserId(article.getUserId());
                article.setNickName(user.getNickname());
                article.setAvatar(user.getAvatar());
                article.setIntroduction(user.getIntroduction());
                //把文章id作为value
                redisTemplate.opsForZSet().add(key+"time",""+article.getArticleId(),time);
                map.put(""+article.getArticleId(), JSON.toJSON(article));
            }
            redisTemplate.opsForHash().putAll(key+"detail",map);
        }
    }
}
